//
//  WCSendNotifyMessageVC.m
//  WC
//
//  Created by wsj2015 on 16/1/25.
//  Copyright © 2016年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

#import "WCSendNotifyMessageVC.h"
#import "WSJPickView.h"
#import "WCCoreDataStore.h"
//#import "WCAddressListViewController.h"
#import "WCUpdateRecordVC.h"
//#import "WCUpdatePatientInforVC.h"


#define patientId_key @"patientId"
#define patientName_key @"patientName"

@interface WCSendNotifyMessageVC ()<UITextViewDelegate,UITextFieldDelegate,UIAlertViewDelegate>

@property (weak, nonatomic) IBOutlet UITextField *messageTitle;
@property (weak, nonatomic) IBOutlet UITextView *contentTextView;
@property (weak, nonatomic) IBOutlet UIButton *sendBtn;



@property (nonatomic,  assign) BOOL isActive;


@property (nonatomic,  retain) NSMutableArray *nurseArray;



@end

@implementation WCSendNotifyMessageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self configUI];

  
    self.isActive = NO;

    
}
- (NSMutableArray *)nurseArray{
    if (_nurseArray ==nil) {
        _nurseArray =[NSMutableArray array];
    }
    return _nurseArray;

}
- (instancetype)init
{
    self = [super init];
    if (self) {
        if (kScreenWidth <=320) {
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
        }
       
    }
    return self;
}
//- (WSJPickView *)picker{
//    if (_picker ==nil ) {
//       _picker =[WSJPickView pickView];
//    
//        defineWeakSelf;
//        _picker.pickTitle = @"患者选择";
//        _picker.saveBlock= ^(NSString *nurseName,NSInteger index){
//            weakSelf.nurseTf.text =nurseName;
//            NSDictionary *dict =weakSelf.nurseArray[index];
//            weakSelf.patientId =[dict[patientId_key] integerValue];
//        };
//
//    }
//    return _picker;
//
//}

- (void)configUI{
//    [self isShowPageBack:YES];
//    [self setTitleLabel:@"发送消息" width:120];
    
    self.title =@"发送消息";
    self.contentTextView.layer.borderColor =kBorderColor.CGColor;
    self.contentTextView.layer.borderWidth =1;
    self.contentTextView.layer.cornerRadius = 10;
    self.contentTextView.layer.masksToBounds = YES;
    self.contentTextView.backgroundColor = kAppBg;
    self.contentTextView.delegate =self;
    
    

}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}
-(void)pageBack{
    UIAlertView *alerView =[[UIAlertView alloc] initWithTitle:@"提示" message:@"是否放弃发送" delegate:self cancelButtonTitle:@"放弃发送" otherButtonTitles:@"继续编辑", nil];
    alerView.tag = 500 +2;
    [alerView show];
    
    

}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];

}
#pragma mark -警告栏事件
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 500 +1) {
        if (buttonIndex == 1) {
            
            [self sendNotifyMessage];
        }
    }else if(alertView.tag ==500 +2){
        if (buttonIndex == 0) {
            [self.navigationController popViewControllerAnimated:YES];;
        }
        
    }else if(alertView.tag ==200){
//        if (buttonIndex ==1) {
//            [self.navigationController pushViewController:[WCAddressListViewController new] animated:YES];
//        }
    }
    
}
- (void)checkParams{
    
    if (self.patientId ==-1) {
        [CoreSVP showSVPWithType:CoreSVPTypeCenterMsg Msg:@"您还没有选择护理师" duration:.8f allowEdit:NO beginBlock:nil completeBlock:nil];
        return;
    }
    if ([self.messageTitle.text isEqualToString:@""]) {
        [CoreSVP showSVPWithType:CoreSVPTypeCenterMsg Msg:@"消息标题不能为空" duration:.8f allowEdit:NO beginBlock:nil completeBlock:nil];
        return;
    }
    
    if ([self.contentTextView.text isEqualToString:@""]) {
        [CoreSVP showSVPWithType:CoreSVPTypeCenterMsg Msg:@"消息内容不能为空" duration:.8f allowEdit:NO beginBlock:nil completeBlock:nil];
        return;
    }
    
    
    
    UIAlertView *alerView =[[UIAlertView alloc] initWithTitle:@"提示" message:@"是否确认提交" delegate:self cancelButtonTitle:@"否" otherButtonTitles:@"确认", nil];
    alerView.tag =500 +1;
    [alerView show];

}


#pragma mark -键盘出现
- (void)keyboardWillShow:(NSNotification *)not
{
    if (self.isActive ==YES) {
        CGRect frame = self.view.frame;
        if (frame.origin.y == 64) {
            frame.origin.y = 0;
        }
        
        self.view.frame =frame;
    }

  

    
}
#pragma mark -键盘隐藏
- (void)keyboardWillHide:(NSNotification *)not
{

    CGRect frame = self.view.frame;
    
    if (frame.origin.y == 0) {
        frame.origin.y =64;
    }
    self.view.frame =frame;
    self.isActive = NO;

    
}



#pragma mark -发送消息 请求
- (void)sendNotifyMessage{
    
//    sourceId 源ID (必填)
//    messageType 消息类型 (3 普通文本消息 必填)
//    messageTitle 消息标题 (必填)
//    messageContent 消息内容 (必填)
//    targetId 接收人ID(必填)
//    targetType 接收人类型 (必填 1、护士 2、患者)
    [WCPopupLayerManager showLodingView];
    NSMutableDictionary *paras =[NSMutableDictionary dictionary];
//    paras[@"sourceId"]  =@([[[[WCDefaultManager shareMg] account] objectForKey:@"id"] integerValue]);
    paras[@"sourceId"] =@([[WCDefaultManager shareMg] nurseId]);
    paras[@"messageType"] =@(3);
    paras[@"messageTitle"] =self.messageTitle.text;
    paras[@"messageContent"] =self.contentTextView.text;
    paras[@"targetId"] =@(self.patientId);
    paras[@"targetType"] =@(2);
    [[WCNetworkAccessManager sharedMg] POST:kSendNotityMessage parameters:paras success:^(NSDictionary *responseObject) {
        CoreSVPSuccess(responseObject[@"data"]);
        [self.navigationController popViewControllerAnimated:YES];
    } failure:nil netError:nil always:^{
        [WCPopupLayerManager dissmissLodingView];
    }];

}


//- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
//{
//    if (textField.text.length >=20) {
//        textField.text = [textField.text substringToIndex:range.location];
//        return NO;
//    }
//    return YES;
//
//}


-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    if (textField ==self.messageTitle) {
        [self.messageTitle resignFirstResponder];
        
    }
    
    return YES;
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    self.isActive =YES;
    
    return YES;

}

//控制输入字符数
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    //回车键 结束
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    if ([text isEqualToString:@"\n"]) {
        //禁止输入换行
        return NO;
    }
    else
    {
        return YES;
    }
    
    return YES;
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.isActive = NO;
    [self.messageTitle resignFirstResponder];
    [self.contentTextView resignFirstResponder];


}

- (IBAction)sendAction:(id)sender {
    [self checkParams];
}




@end
